package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Role;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-18 15:53
 * @descreption 经办人办理接口
 **/
@Component("handlerService1.0")
@NotNull(name = "id")
public class HandlerService extends AbstractApiService {

    /**
     * 1.填写办理情况(合格/不合格)
     * 2.更新表数据
     * 3.提交给审批领导进行审批
     */

    @Override
    public Map service(Map request) {
        //查表
        DeclareInfo declareInfo = Mongo.buildMongo().eq("_id", new ObjectId(id(request))).findOne(DeclareInfo.class);
        //查看该地区的分工领导
        User manager = Mongo.buildMongo().eq("areaInfo.countyCode", declareInfo.getAreaInfo().getCountyCode()) .eq("role",Role.DIST_HEADER).findOne(User.class);

        //设置提交,发送给审批领导
        Mongo.buildMongo().eq("_id", new ObjectId(id(request))).updateFirst(update ->
        {
            update.set("declareStatus", declareInfo.getDeclareStatus().handler(whether(request)));
            update.set("mainManagerId", manager.getId());
            update.set("otherManagerId", "");
            update.set("updateAt", System.currentTimeMillis() / 1000);

        }, DeclareInfo.class);

        declareLog(id(request));
        return ApiResponse.success();

    }




}